using System;
using System.Collections;
using UnityEngine;

namespace PKGames.AssetBundleSystem
{
	public class Coroutine
	{
		private IEnumerator _enumerator;

		private Exception _exception;

		public Coroutine(IEnumerator enumerator)
		{
			_enumerator = enumerator;
		}

		public void CheckError()
		{
			if (_exception != null)
			{
				throw _exception;
			}
		}

		public bool HasError()
		{
			return _exception != null;
		}

		private IEnumerator InternalRoutine()
		{
			while (true)
			{
				try
				{
					if (!_enumerator.MoveNext())
					{
						yield break;
					}
				}
				catch (Exception exception)
				{
					Exception ex = _exception = exception;
					yield break;
				}
				yield return _enumerator.Current;
			}
		}

		public IEnumerator Start(MonoBehaviour worker)
		{
			yield return worker.StartCoroutine(InternalRoutine());
		}
	}
}
